//
//  UpdCustomerViewController.m
//  Kfangchan
//
//  Created by liran on 14/12/8.
//  Copyright (c) 2014年 Beijing Renjuren Network Technology Co., Ltd. All rights reserved.
//

#import "UpdCustomerViewController.h"

@interface UpdCustomerViewController ()

@property (strong, nonatomic) IBOutlet UIScrollView *scrollView;

@property (strong, nonatomic) IBOutlet UIView *nameView;
@property (strong, nonatomic) IBOutlet UIView *mobileView;
@property (strong, nonatomic) IBOutlet UIView *idcardView;
@property (strong, nonatomic) IBOutlet UIView *remarkView;
@property (strong, nonatomic) IBOutlet UIView *seeBookView;
@property (strong, nonatomic) IBOutlet UIView *seeBookTimeView;
@property (strong, nonatomic) IBOutlet UIView *seeBookCarView;

@property (strong, nonatomic) IBOutlet UILabel *nameLabel;
@property (strong, nonatomic) IBOutlet UILabel *mobileLabel;
@property (strong, nonatomic) IBOutlet UIImageView *sexImageView;

@property (strong, nonatomic) IBOutlet UITextField *idcardTextField;
@property (strong, nonatomic) IBOutlet UITextView *remarkTextView;
@property (strong, nonatomic) IBOutlet UILabel *remarkPlaceHolderLabel;
@property (strong, nonatomic) IBOutlet UISwitch *seeBookSwitch;
@property (strong, nonatomic) IBOutlet UITextField *seeBookTimeTextField;
@property (strong, nonatomic) IBOutlet UISwitch *seeBookCarSwitch;

@property (strong, nonatomic) IBOutlet UIButton *recommendButton;

@property (strong, nonatomic) IBOutlet UIView *dateBgView;
@property (strong, nonatomic) IBOutlet UIView *dateView;
@property (strong, nonatomic) IBOutlet UIDatePicker *datePicker;

@end

@implementation UpdCustomerViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.titleView = [[Theam currentTheam] navigationTitleViewWithTitle:@"推荐客户"];
    self.navigationItem.leftBarButtonItem = [[Theam currentTheam] navigationBarLeftButtonItemWithImage:[UIImage imageNamed:@"nav_back.png"] Target:self Selector:@selector(back)];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideKeyboard)];
    [tap setNumberOfTouchesRequired:1];
    [self.view addGestureRecognizer:tap];
    
    self.nameLabel.text = self.customer.name;
    self.mobileLabel.text = self.customer.mobile;
    if ([self.customer.sex isEqualToString:@"男"]) {
        self.sexImageView.image = [UIImage imageNamed:@"new_customer_male_normal.png"];
    } else {
        self.sexImageView.image = [UIImage imageNamed:@"new_customer_female_normal.png"];
    }
    self.idcardTextField.text = self.customer.idcard;
    if (self.customer.idcard != nil && self.customer.idcard.length > 0) {
        self.idcardTextField.userInteractionEnabled = NO;
        self.idcardView.backgroundColor = [UIColor colorWithHexString:@"#DCDCDC"];
    }
    if (self.customer.remark != nil && self.customer.remark.length > 0) {
        self.remarkTextView.text = self.customer.remark;
        self.remarkPlaceHolderLabel.text = @"";
    }
    
    self.nameView.layer.cornerRadius = 4.0;
    self.mobileView.layer.cornerRadius = 4.0;
    self.idcardView.layer.cornerRadius = 4.0;
    self.remarkView.layer.cornerRadius = 4.0;
    self.seeBookView.layer.cornerRadius = 4.0;
    self.seeBookTimeView.layer.cornerRadius = 4.0;
    self.seeBookCarView.layer.cornerRadius = 4.0;
    
    self.scrollView.size = [UIScreen mainScreen].bounds.size;
    [self.scrollView setContentSize:CGSizeMake(self.scrollView.width, self.recommendButton.origin.y + self.recommendButton.size.height + 80)];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)textViewDidChange:(UITextView *)textView {
    if (self.remarkTextView.text.length == 0) {
        self.remarkPlaceHolderLabel.text = @"填写客户购房需求";
    }else{
        self.remarkPlaceHolderLabel.text = @"";
    }
}

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
    if ([text isEqualToString:@"\n"]) {
        [self.remarkTextView resignFirstResponder];
        return NO;
    }
    return YES;
}

#pragma mark 返回
#pragma mark
- (void)back {
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark -
#pragma mark 关闭键盘
- (IBAction)hideKeyboard {
    [self.idcardTextField resignFirstResponder];
    [self.remarkTextView resignFirstResponder];
}

#pragma mark -
#pragma mark 是否自行带看切换
- (IBAction)seeBookChange:(UISwitch *)sender {
    float y = 0;
    if (sender.on) {
        self.seeBookTimeView.hidden = NO;
        y = self.seeBookTimeView.origin.y + self.seeBookTimeView.size.height + 8;
    } else {
        self.seeBookTimeView.hidden = YES;
        y = self.seeBookTimeView.origin.y;
    }
    
    __weak UpdCustomerViewController *weak_self = self;
    [UIView animateWithDuration:0.5 animations:^{
        weak_self.seeBookCarView.origin = CGPointMake(self.seeBookCarView.origin.x, y);
        weak_self.recommendButton.origin = CGPointMake(self.recommendButton.origin.x, self.seeBookCarView.origin.y + self.seeBookCarView.size.height + 8);
    }];
}

#pragma mark -
#pragma mark 时间选择器
- (IBAction)showDatePicker:(id)sender {
    self.dateBgView.hidden = NO;
    __weak UpdCustomerViewController *weak_self = self;
    [UIView animateWithDuration:0.5 animations:^{
        weak_self.dateView.origin = CGPointMake(0, ScreenHeight - weak_self.dateView.size.height - weak_self.navigationController.navigationBar.size.height);
    }];
}

- (IBAction)hideDatePicker:(id)sender {
    __weak UpdCustomerViewController *weak_self = self;
    [UIView animateWithDuration:0.5 animations:^{
        weak_self.dateView.origin = CGPointMake(0, ScreenHeight);
    } completion:^(BOOL finished) {
        self.dateBgView.hidden = YES;
    }];
}

- (IBAction)selectDatePicker:(id)sender {
    NSDate *date = [self.datePicker date];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm"];
    self.seeBookTimeTextField.text = [formatter stringFromDate:date];
    [self hideDatePicker:nil];
}

#pragma mark -
#pragma mark 推荐
- (IBAction)recommend {
    BOOL verify = [self verify];
    if (verify) {
        NSDictionary *param = @{
                                @"customerId": [NSString stringWithFormat:@"%i", self.customer.pkid],
                                @"buildingId": [NSString stringWithFormat:@"%i", self.buildingId],
                                @"idcard": self.idcardTextField.text,
                                @"remark": self.remarkTextView.text,
                                @"seeBook": [NSString stringWithFormat:@"%i", self.seeBookSwitch.on],
                                @"seeBookTime": self.seeBookTimeTextField.text,
                                @"seeBookCar": [NSString stringWithFormat:@"%i", self.seeBookCarSwitch.on],
                                };
        MKNetworkOperation *operation = [[BaseEngine sharedEngine]RunRequest:[param mutableCopy] path:KFC_RECOMMEND_CUSTOMER completionHandler:^(id responseObject) {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"推荐成功" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
            [alert show];
            theApp.reloadCustomerList = YES;
        } errorHandler:^(NSError *error) {
            showCustomAlertMessage(@"网路错误");
        } finishHandler:^(id responseObject) {
            
        }];
        [self.opHandlers addObject:operation];
    }
}

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    int count = [[self.navigationController viewControllers] count];
    if (count > 3) {
        [self.navigationController popToViewController:[self.navigationController viewControllers][count - 4] animated:YES];
    }
}

#pragma mark 表单验证
- (BOOL)verify {
    NSString *idcard = self.idcardTextField.text;
    NSString *remark = self.remarkTextView.text;
    BOOL seeBook = self.seeBookSwitch.on;
    NSString *seeBookTime = self.seeBookTimeTextField.text;
    
    UIAlertView *alert = nil;
    // 身份证(选填)
    if(idcard.length > 0){
        NSString *regex_idcard = @"^(\\d{14}|\\d{17})(\\d|[xX])$";;
        NSArray *regexArray = [idcard componentsMatchedByRegex:regex_idcard];
        if(regexArray.count == 0){
            alert = [[UIAlertView alloc]initWithTitle:nil message:@"请输入正确的身份证号" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
            [alert show];
            return NO;
        }
    }
    
    // 备注
    if (remark.length > 200) {
        alert = [[UIAlertView alloc] initWithTitle:nil message:@"备注限定200字以内" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
        [alert show];
        return NO;
    }
    
    // 带看时间
    if (seeBook) {
        if (seeBookTime.length == 0) {
            alert = [[UIAlertView alloc] initWithTitle:nil message:@"请选择带看时间" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
            [alert show];
            return NO;
        }
    }
    
    return YES;
}

@end
